package cn.fancy.sdk.entity;

import org.json.JSONObject;

import cn.fancy.sdk.common.utils.Logger;
import cn.fancy.sdk.login.utils.Req;


import android.content.Context;
import android.content.SharedPreferences;



public class Session implements JsonParseInterface {
	
	
	public Session(){}
	
	@Override
	public JSONObject buildJson() 
	{

		JSONObject json=new JSONObject();
	
		return json;

	}

	@Override
	public String getShortName() 
	{
		return "Session";
	}
	
	
	@Override
	public void parseJson(JSONObject json) 
	{
		
		if(json==null) return;
		
		JSONObject myJason = (JSONObject)json.opt(getShortName());
		
		if(myJason==null || Req.context==null) return;
		
		SharedPreferences sp = Req.context.getSharedPreferences("csdksession", Context.MODE_PRIVATE);
				
		if(sp!=null)
		{
			
			Logger.print_red("-------更新SDK Session-------");
			
			String userAccount = myJason.optString("a","").toString();
			String sessionId = myJason.optString("b","").toString();
			int userId = myJason.optInt("c",Integer.MIN_VALUE);
			String mobilePhone = myJason.optString("d","").toString();
			int isVIP = myJason.optInt("e",Integer.MIN_VALUE);
			String email = myJason.optString("f","").toString();
			String external=myJason.optString("g","").toString();
					
				
			SharedPreferences.Editor editor = sp.edit();
			editor.putString("a", userAccount);
			editor.putString("b", sessionId);
			editor.putInt("c", userId);
			editor.putString("d", mobilePhone);
			editor.putInt("e",  isVIP);
			editor.putString("f", email);
			editor.putString("g", external);
			editor.commit();		
		}	
			
			
			
	}
	
	
	
	public static void clear(Context context)
	{
		
		if(context == null) return;
		
		SharedPreferences sp = context.getSharedPreferences("csdksession", Context.MODE_PRIVATE);
		
		if(sp!=null)
		{
			SharedPreferences.Editor editor = sp.edit();
			editor.putString("a", "");
			editor.putString("b", "");
			editor.putInt("c",Integer.MIN_VALUE);
			editor.putString("d","");
			editor.putInt("e",  Integer.MIN_VALUE);
			editor.putString("f", "");
			editor.putString("g", "");
			editor.commit();		
		}
	}
	

}
